package com.jky.trial.controller;

import com.jky.trial.common.BaseResult;
import com.jky.trial.common.ListResult;
import com.jky.trial.common.jwt.JWTUtil;
import com.jky.trial.entity.Msg;
import com.jky.trial.entity.User;
import com.jky.trial.service.MsgService;
import com.jky.trial.vo.MsgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/msg")
public class MsgController {

    private MsgService recordService;

    @Autowired
    MsgController(MsgService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    public BaseResult create(@RequestBody Msg record) {
        recordService.create(record);
        return new BaseResult(true, record.getId());
    }

    @PutMapping
    public BaseResult update(@RequestBody Msg record) {
        return new BaseResult(true, recordService.update(record));
    }

    @DeleteMapping("/{id}")
    public BaseResult del(@PathVariable Integer id) {
        return new BaseResult(true, recordService.del(id));
    }

    @GetMapping("/{id}")
    public BaseResult get(@PathVariable Integer id) {
        return new BaseResult(true, recordService.detail(id));
    }

    @GetMapping
    public ListResult page(MsgVo record) {
        return recordService.page(record);
    }

    @GetMapping("/pageWithFromUserName")
    public ListResult pageWithFromUserName(MsgVo record, HttpServletRequest request) {
        User user = JWTUtil.getUser(request.getHeader("tk"));
        record.setToUser(user.getId());
        return recordService.pageListWithFromUserName(record);
    }

    @GetMapping("/list")
    public ListResult list(MsgVo record) {
        return new ListResult(true, recordService.list(record));
    }
}
